package com.example.my

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.activity.ComponentActivity

const val FIRST_MSG = "FIRST_MSG"

const val SECOND_MSG = "SECOND_MSG"
class MainActivity : ComponentActivity() {

    private lateinit var textView: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val first = findViewById<Button>(R.id.first)
        val editText = findViewById<EditText>(R.id.editText)

        val second = findViewById<Button>(R.id.second)
        textView = findViewById(R.id.textView)
        val third = findViewById<Button>(R.id.third)
        val four = findViewById<Button>(R.id.four)

        first.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
           intent.putExtra(FIRST_MSG, editText.text.toString())
            startActivity(intent)
        }

        second.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)
            startActivityForResult(intent, 0)
        }

        third.setOnClickListener {
            val intent = Intent(this, FourthActivity::class.java)
            startActivity(intent)
        }

        four.setOnClickListener {
            val intent = Intent(this, FifthActivity::class.java)
            startActivity(intent)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?, ) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 0) {
            if (resultCode == 1) {
                textView.text = data?.getStringExtra(SECOND_MSG)
            }
        }
    }
}
